#pragma once

#include "Defines.h"
#include <irrlicht.h>
#include <string>
// BOOST INCLUDES
#include <boost/thread/thread.hpp>
#include "SocketCommunicator.h"
#include "Serializer.h"

class NetworkHandler
{
	public:
		Serializer serializer;
		SocketCommunicator socketCommunicator;
		bool hasConnection;
		
		std::map<std::string, PlayerObject*> clientPlayerObjectMap;
	private:
		boost::shared_ptr<boost::thread> thread;
		unsigned int delay;

	public:
		NetworkHandler();

		PlayerObject* getPlayerObjectFromName(const std::string& name);
	
		std::vector<NetworkPacket> sendQueue;
		std::vector< std::pair<NetworkPacket, ClientAddress> > receivedQueue;
		
		void start();
		void stop();
		bool interruptWait();
		//void sendMessages(); //maybe destination as ip,port parameter

		void addToSendQueue(uintptr_t pointer, HeaderType headerType);
		void addToReceivedQueue(const std::string& serializedData, ClientAddress* clientAddress);
		void clearSendQueue();

		bool getHasConnection();
		void setHasConnection(bool passedHasConnection);
		void setDelay(unsigned int passedDelay);
		virtual void handleReceivedMessages() = 0;
	private:
		void threadLoop();
//		virtual void waitLoop() = 0;
		virtual void prepareSendQueue() = 0;
		virtual void sendMessages() = 0;
};

extern NetworkHandler* globalCSHandler;
